<!DOCTYPE html>
<html xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8">
    <title>用户管理</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
    <style type="text/css">
        .layui-form-select dl {
            max-height: 170px;
        }
    </style>
</head>
<body class="pear-container">

<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">所属部门:</label>
                    <div class="layui-input-inline">
                        <select id="depId" name="depId">
                            <option value="">请选择所属部门</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">用户名称:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="realName" placeholder="请输入用户名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">电话号码:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="phone" placeholder="请输入电话号码" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">开始时间:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="beginTime" placeholder="请选择开始时间" id="beginTime" readonly="readonly"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">结束时间:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="endTime" placeholder="请选择结束时间" id="endTime" readonly="readonly"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">状&nbsp;&nbsp;&nbsp;&nbsp;态:</label>
                    <div class="layui-input-inline">
                        <select id="state" name="state">
                            <option value="">可用状态</option>
                            <option value="1">正常</option>
                            <option value="2">停用</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline" style="margin-left: 60px;">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<!--添加和修改弹出层-->
<div style="display: none;padding: 3px" id="saveOrUpdateDiv">
    <form class="layui-form" id="dataFrm" lay-filter="dataFrm" action="">
        <div class="layui-form-item" style="margin-top: 10px;">
            <div class="layui-inline">
                <label class="layui-form-label">用户姓名:</label>
                <div class="layui-input-inline">
                    <input type="hidden" name="id">
                    <input type="text" name="realName" lay-verify="required" autocomplete="off"
                           placeholder="请输入用户姓名" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">登录名:</label>
                <div class="layui-input-inline">
                    <input type="text" name="userName" lay-verify="required" autocomplete="off"
                           placeholder="请输入登录名" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">手机号:</label>
                <div class="layui-input-inline">
                    <input type="text" name="phone" lay-verify="required" autocomplete="off"
                           placeholder="请输入手机号"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">年龄:</label>
                <div class="layui-input-inline">
                    <input type="text" name="age" lay-verify="required|number" autocomplete="off"
                           placeholder="请输入年龄"
                           class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">所属科室:</label>
                <div class="layui-input-inline">
                    <select id="department" name="depId">
                        <option value="">所属科室</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">级别:</label>
                <div class="layui-input-inline">
                    <select id="level" name="level">
                        <option value="">用户级别</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">背景学历:</label>
                <div class="layui-input-inline">
                    <select id="education" name="education">
                        <option value="">背景学历</option>
                        <option value="无">无</option>
                        <option value="专科">专科</option>
                        <option value="本科">本科</option>
                        <option value="研究生">研究生</option>
                        <option value="博士">博士</option>
                        <option value="研究员">研究员</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">用户类型:</label>
                <div class="layui-input-inline">
                    <select id="type" name="type">
                        <option value="">用户类型</option>
                    </select>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">性别:</label>
                <div class="layui-input-inline">
                    <input type="radio" name="sex" value="1" title="男" checked>
                    <input type="radio" name="sex" value="2" title="女">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">状态:</label>
                <div class="layui-input-inline">
                    <input type="radio" name="state" value="1" title="正常" checked>
                    <input type="radio" name="state" value="2" title="停用">
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                        lay-filter="user-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" onclick="reset()" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
</div>

<!-- 分配权限弹层 -->
<div style="display: none;padding: 3px" id="saveOrRoleDiv">
    <!-- 分配角色  数据表格 -->
    <table id="roleTable" lay-filter="userTable"></table>
</div>

<!--数据表格-->
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<!--表头-->
<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add" shiro:hasPermission="User:create">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-warming pear-btn-md" lay-event="resetPassword"
            shiro:hasPermission="User:restPassword">
        <i class="layui-icon layui-icon-password"></i>
        重置密码
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove" shiro:hasPermission="User:bathDelete">
        <i class="layui-icon layui-icon-delete"></i>
        批量删除
    </button>
</script>

<!--行工具栏-->
<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit" shiro:hasPermission="User:update"><i
            class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-warming pear-btn-sm" lay-event="role" shiro:hasPermission="User:distributionRole">
        <i class="layui-icon layui-icon-auz"></i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove" shiro:hasPermission="User:delete"><i
            class="layui-icon layui-icon-delete"></i>
    </button>
</script>

<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'common', 'laydate'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let laydate = layui.laydate;
        let common = layui.common;

        //渲染时间选择器
        laydate.render({
            elem: '#beginTime',
            type: 'datetime'
        });
        laydate.render({
            elem: '#endTime',
            type: 'datetime'
        });

        //加载查询条件的下拉框
        $.get("/department/loadAllDepartment", function (data) {
            let depId = $("#depId");
            let html = '<option value="">请选择所属部门</option>'
            $.each(data.data, function (i, item) {
                html += '<option value="' + item.id + '">' + item.name + '</option>'
            });
            depId.html(html);
            form.render();
        });

        let cols = [
            [{
                type: 'checkbox'
            },
                {
                    title: '用户ID',
                    field: 'id',
                    align: 'center',
                    hide: true,
                    width: '80',
                },
                {
                    title: '用户姓名',
                    field: 'realName',
                    align: 'center',
                    width: '100',
                },
                {
                    title: '登录名',
                    field: 'userName',
                    align: 'center',
                    width: '100',
                },
                {
                    title: '部门',
                    field: 'depName',
                    align: 'center',
                    width: '60',
                },
                {
                    title: '手机号码',
                    field: 'phone',
                    align: 'center',
                    width: '110',
                },
                {
                    title: '性别',
                    field: 'sex',
                    width: '60',
                    align: 'center',
                    templet: function (d) {
                        return d.sex == 1 ? '<font color=blue>男</font>' : '<font color=red>女</font>';
                    }
                },
                {
                    title: '年龄',
                    field: 'age',
                    width: '60',
                    align: 'center',
                },
                {
                    title: '级别',
                    width: '100',
                    field: 'levelName',
                    align: 'center',
                },
                {
                    title: '背景',
                    width: '80',
                    field: 'education',
                    align: 'center',
                },
                {
                    title: '状态',
                    width: '100',
                    field: 'state',
                    align: 'center',
                    templet: function (d) {
                        let strCheck = d.state == "1" ? "checked" : "";
                        return '<input type="checkbox" name="state" lay-skin="switch" lay-text="正常|停用" lay-filter="user-enable" ' + strCheck + ' id=' + d.id + '>';
                    }
                },
                {
                    title: '用户类型',
                    width: '100',
                    field: 'userTypeName',
                    align: 'center',
                },
                {
                    title: '创建时间',
                    width: '130',
                    field: 'createTime',
                    align: 'center',
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'center',
                    width: 160,
                }
            ]
        ]

        let tableIns = table.render({
            elem: '#user-table',
            url: '/user/loadAllUser',
            page: true,
            cols: cols,
            page: true,
            skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print'],
            response: {
                statusCode: 200
            },
            done: function (res, curr, count) {
                if (res.data.length == 0 && curr != 1) {
                    tableIns.reload({
                        page: {
                            curr: (curr - 1)
                        }
                    });
                }
            },
        });

        //行监听
        table.on('tool(user-table)', function (obj) {
            if (obj.event === 'remove') {
                remove(obj);
            } else if (obj.event === 'role') {
                role(obj.data);
            } else if (obj.event === 'edit') {
                edit(obj);
            }
        });

        //表头监听
        table.on('toolbar(user-table)', function (obj) {
            if (obj.event === 'add') {
                add();
            } else if (obj.event === 'resetPassword') {
                resetPassword(obj);
            } else if (obj.event === 'batchRemove') {
                batchRemove(obj);
            } else if (obj.event === 'refresh') {
                window.refresh();
            }
        });

        //表格监听
        form.on('submit(user-query)', function (data) {
            table.reload('user-table', {
                where: data.field
            })
            return false;
        });

        //   行 点击监听
        form.on('switch(user-enable)', function (obj) {
            let id = $(this).attr('id');
            let state = '';
            obj.elem.checked ? state = '1' : state = '2';
            $.ajax({
                url: "/user/updateUserState/" + id + "/" + state,
                dataType: 'json',
                type: 'post',
                success: function (result) {
                    if (result.code == 200) {
                        layer.msg(result.msg, {
                            icon: 1,
                            time: 1000
                        });
                        tableIns.reload();
                    } else {
                        layer.msg(result.msg, {
                            icon: 2,
                            time: 1000
                        });
                    }
                }
            })

        });

        //添加
        let mainIndex;
        let url;

        function add() {
            mainIndex = layer.open({
                type: 1,
                title: '添加用户信息',
                shade: 0.1,
                area: ['650px', '510px'],
                content: $("#saveOrUpdateDiv"),
                success: function (index) {
                    url = "/user/addUser";
                    $("#dataFrm")[0].reset();

                    //加载科室的下拉框
                    $.ajax({
                        url: "/department/loadAllDepartment",
                        type: "GET",
                        async: false,
                        success: function (data){
                            let department = $("#department");
                            let html = '<option value="">所属科室</option>'
                            $.each(data.data, function (i, item) {
                                html += '<option value="' + item.id + '">' + item.name + '</option>'
                            });
                            department.html(html);
                        }

                    })


                    //加载用户级别的下拉框
                    $.ajax({
                        url:"/user/loadUserLevel",
                        type: "GET",
                        async: false,
                        success: function(data){
                            let level = $("#level");
                            let html = '<option value="">级别</option>'
                            $.each(data.data, function (i, item) {
                                html += '<option value="' + item.id + '">' + item.name + '</option>'
                            });
                            level.html(html);
                        }
                    })

                    //加载用户类型的下拉框
                    $.ajax({
                        url: "/user/loadUserType",
                        type: "GET",
                        async: false,
                        success: function (data){
                            let type = $("#type");
                            let html = '<option value="">用户类型</option>'
                            $.each(data.data, function (i, item) {
                                html += '<option value="' + item.id + '">' + item.name + '</option>'
                            });
                            type.html(html);
                        }
                    })
                    form.render();
                }
            });
        }


        //修改
        function edit(obj) {
            mainIndex = layer.open({
                type: 1,
                title: '修改用户信息',
                shade: 0.1,
                area: ['650px', '510px'],
                content: $("#saveOrUpdateDiv"),
                success: function (index) {
                    url = "/user/updateUser"
                    $("#dataFrm")[0].reset();
                    form.val("dataFrm", obj.data);

                    //加载科室的下拉框
                    $.ajax({
                        url: "/department/loadAllDepartment",
                        type: "GET",
                        async: false,
                        success: function (data){
                            let department = $("#department");
                            let html = '<option value="">所属科室</option>'
                            $.each(data.data, function (i, item) {
                                if (item.id === obj.data.depId) {
                                    html += '<option value="' + item.id + '" selected=selected>' + item.name + '</option>'
                                } else {
                                    html += '<option value="' + item.id + '">' + item.name + '</option>'
                                }

                            });
                            department.html(html);
                        }
                    })

                    //加载用户级别的下拉框
                    $.ajax({
                        url: "/user/loadUserLevel",
                        type: "GET",
                        async: false,
                        success: function (data){
                            let level = $("#level");
                            let html = '<option value="">级别</option>'
                            $.each(data.data, function (i, item) {
                                if (item.id === obj.data.level) {
                                    html += '<option value="' + item.id + '" selected=selected>' + item.name + '</option>'
                                } else {
                                    html += '<option value="' + item.id + '">' + item.name + '</option>'
                                }
                            });
                            level.html(html);
                        }
                    })

                    //加载用户类型的下拉框
                    $.ajax({
                        url: "/user/loadUserType",
                        type: "GET",
                        async: false,
                        success: function (data){
                            let type = $("#type");
                            let html = '<option value="">用户类型</option>'
                            $.each(data.data, function (i, item) {
                                if (item.id === obj.data.type) {
                                    html += '<option value="' + item.id + '" selected=selected>' + item.name + '</option>'
                                } else {
                                    html += '<option value="' + item.id + '">' + item.name + '</option>'
                                }
                            });
                            type.html(html);
                        }
                    })

                    form.render();
                }
            });
        }

        //监听提交按钮
        form.on('submit(user-save)', function (data) {
            $.ajax({
                url: url,
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.code == 200) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            layer.close(mainIndex);//关闭当前页
                            tableIns.reload();
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });

        //删除
        function remove(obj) {
            layer.confirm('确定要删除?', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/user/deleteUser/" + obj.data['id'],
                    dataType: 'json',
                    type: 'DELETE',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            });
                            tableIns.reload();
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        function resetPassword(obj) {
            let ids = common.checkField(obj, 'id');

            if (ids === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定要重置密码吗?', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/user/resetUserPassword/" + ids,
                    dataType: 'json',
                    type: 'GET',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            });
                            tableIns.reload();
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        //批量删除
        function batchRemove(obj) {

            let ids = common.checkField(obj, 'id');

            if (ids === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定要删除?', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/user/bathDeleteUser/" + ids,
                    dataType: 'json',
                    type: 'DELETE',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            });
                            tableIns.reload();
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        function role(data) {
            mainIndex = layer.open({
                type: 1,
                title: '分配【' + data.realName + '】的角色',
                area: ['700px', '530px'],
                content: $("#saveOrRoleDiv"),
                btn: ['确认', '取消'],
                btnAlign: 'c',
                success: function (index) {
                    table.render({
                        elem: '#roleTable',
                        url: '/user/loadRolesByUserId?id=' + data.id,
                        cols: [
                            [
                                {type: 'checkbox'},
                                {field: 'id', title: 'ID', align: 'center'},
                                {field: 'name', title: '角色', align: 'center'},
                                {field: 'remark', title: '角色备注', align: 'center'},
                            ]
                        ],
                        page: false,
                        response: {
                            statusCode: 200
                        }
                    });
                },
                yes: function (index, layero) {
                    let checkStatus = table.checkStatus('roleTable');
                    let ids = "userId=" + data.id;
                    $.each(checkStatus.data, function (i, item) {
                        ids += "&ids=" + item.id;
                    });
                    $.ajax({
                        url: "/user/saveRoleUser",
                        dataType: 'json',
                        data: ids,
                        type: 'POST',
                        success: function (result) {
                            layer.close(mainIndex);
                            if (result.code == 200) {
                                layer.msg(result.msg, {
                                    icon: 1,
                                    time: 1000
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                }
            });
        }


        window.refresh = function (param) {
            table.reload('user-table');
        }

    })
</script>
</body>
</html>
